package com.mooer.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.mooer.R;
import com.mooer.bean.Comment;
import com.mooer.exception.SVException;
import com.mooer.http.HttpUtils;
import com.mooer.utils.ui.BitmapViewBinder;
import com.mooer.utils.ui.ProgressLinearLayout;
import com.mooer.utils.ui.UiUtils;

/**
 * @author pengfei.xu
 * @version 创建时间：2011-6-22 下午02:04:53 类说明
 */
public class ActivityDetail extends Activity implements OnClickListener {

	private ImageView sign, remark;

	private ListView listView;

	private ImageView activityPic;

	private TextView activityName, activityRequire;

	private SimpleAdapter adapter;

	private ProgressLinearLayout footer;

	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

	private Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_detail);
		intent = getIntent();
		activityName = (TextView) findViewById(R.id.activity_name);
		activityRequire = (TextView) findViewById(R.id.activity_require);
		sign = (ImageView) findViewById(R.id.sign);
		remark = (ImageView) findViewById(R.id.remark);
		activityPic = (ImageView) findViewById(R.id.activity_pic);
		listView = (ListView) findViewById(R.id.listview);
		footer = (ProgressLinearLayout) LayoutInflater.from(this).inflate(
				R.layout.progress_small, null);
		footer.initView();
		listView.addFooterView(footer);
		adapter = new SimpleAdapter(this, list, R.layout.activity_comment_item,
				new String[] { "pic", "name", "content", "date" }, new int[] {
						R.id.pic, R.id.name, R.id.comment_content,
						R.id.comment_date });
		adapter.setViewBinder(new BitmapViewBinder());
		listView.setAdapter(adapter);
		activityName.setText(intent.getStringExtra("title"));
		activityRequire.setText(intent.getStringExtra("content"));

		sign.setOnClickListener(this);
		remark.setOnClickListener(this);
		footer.setOnClickListener(this);

		new ActivityDetailTask().execute(intent.getIntExtra("activityId", 0));
	}

	@Override
	public void onClick(View v) {
		if (v == sign) {
			UiUtils.showToast(this,
					"报名成功。详细内容及最终中选情况\n请登录www.moorecat.com活动专区查询");
		}

	}

	public class ActivityDetailTask extends
			AsyncTask<Integer, Integer, List<Comment>> {

		private boolean isException;

		@Override
		protected List<Comment> doInBackground(Integer... params) {
			List<Comment> list = null;
			try {
				list = HttpUtils
						.fetchShopActivityCommentList(params[0], 0, 100);
			} catch (Exception e) {
				isException = true;
				e.printStackTrace();
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<Comment> result) {
			super.onPostExecute(result);
			if (isException) {
				footer.setVisibility(View.GONE, View.GONE, View.VISIBLE,
						View.VISIBLE);
			} else {
				if (result.size() > 0) {
					for (Comment comment : result) {
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("pic",
								getResources().getDrawable(
										R.drawable.avatar_pic_x));
						map.put("name", comment.getUser().getUserName());
						map.put("content", comment.getContent());
						map.put("date", comment.getDate());
						list.add(map);
					}
					listView.removeFooterView(footer);
					adapter.notifyDataSetChanged();
				} else {
					footer.setVisibility(View.GONE, View.GONE, View.VISIBLE,
							View.GONE);
					footer.setText(null, "本活动暂无评论");
				}
			}
		}
	}

}
